package com.ruoyi.kucunpandian.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.cangku.domain.Cangku;
import com.ruoyi.cangku.service.ICangkuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kucunpandian.domain.Kucunpandian;
import com.ruoyi.kucunpandian.service.IKucunpandianService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 库存盘点Controller
 * 
 * @author ruoyi
 * @date 2023-01-10
 */
@Controller
@RequestMapping("/kucunpandian/kucunpandianTest")
public class KucunpandianController extends BaseController
{
    private String prefix = "kucunpandian/kucunpandianTest";

    @Autowired
    private IKucunpandianService kucunpandianService;
    @Autowired
    private ICangkuService cangkuService;

    @RequiresPermissions("kucunpandian:kucunpandianTest:view")
    @GetMapping()
    public String kucunpandianTest()
    {
        return prefix + "/kucunpandianTest";
    }

    /**
     * 查询库存盘点列表
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, Kucunpandian kucunpandian)
    {
        startPage();
        String a = request.getSession().getAttribute("QuanXian").toString();
        String b = request.getSession().getAttribute("name4user").toString();
        if (a.equals("0") && (kucunpandian == null || kucunpandian.getCangkuName().equals(""))) {
            List<Kucunpandian> namee2cangku = kucunpandianService.selectAllKucunpandianList(b);

            return getDataTable(namee2cangku);
            /*List<Kucunpandian> gouhuoRukuList = new ArrayList<>();
            for (Kucunpandian c:namee2cangku) {
                if (c.getCangkuName().equals(request.getSession().getAttribute("name4user").toString())) {
                    gouhuoRukuList.add(c);
                }
            }
            return getDataTable(gouhuoRukuList);*/
        }else {
            startPage();
            List<Kucunpandian> list = kucunpandianService.selectKucunpandianList(kucunpandian);

            /*String s=list.toString();
            JSONObject jsonObject= JSON.parseObject(s);
            System.out.println(jsonObject+"----------------------------------------------------------------------------------------------------------");
            */
            return getDataTable(list);

        }
    }

    /**
     * 导出库存盘点列表
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:export")
    @Log(title = "库存盘点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Kucunpandian kucunpandian)
    {
        List<Kucunpandian> list = kucunpandianService.selectKucunpandianList(kucunpandian);
        ExcelUtil<Kucunpandian> util = new ExcelUtil<Kucunpandian>(Kucunpandian.class);
        return util.exportExcel(list, "库存盘点数据");
    }

    /**
     * 新增库存盘点
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存盘点
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:add")
    @Log(title = "库存盘点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Kucunpandian kucunpandian)
    {
        return toAjax(kucunpandianService.insertKucunpandian(kucunpandian));
    }

    /**
     * 修改库存盘点
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Kucunpandian kucunpandian = kucunpandianService.selectKucunpandianById(id);
        mmap.put("kucunpandian", kucunpandian);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存盘点
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:edit")
    @Log(title = "库存盘点", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Kucunpandian kucunpandian)
    {
        return toAjax(kucunpandianService.updateKucunpandian(kucunpandian));
    }

    /**
     * 删除库存盘点
     */
    @RequiresPermissions("kucunpandian:kucunpandianTest:remove")
    @Log(title = "库存盘点", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(kucunpandianService.deleteKucunpandianByIds(ids));
    }
}
